#include<head.h>

int main(int argc, char const *argv[])
{
    if (argc !=2)
    {
        printf("格式错误: %s [port]\n",argv[0]);
        exit(1);        
    }
    
    // 创建用户数据报 套接字 
    int sock_fd  = socket(AF_INET ,SOCK_DGRAM , 0);
    if (-1 == sock_fd)
    {
        ERRLOG("套接字创建错误");
        return 0 ; 
    }
    
    struct sockaddr_in server_addr;

    memset(&server_addr , 0 ,sizeof(server_addr));

    server_addr.sin_family = AF_INET;

    server_addr.sin_port = htons(atoi(argv[1]));

    server_addr.sin_addr.s_addr = inet_addr("192.168.20.56");

    socklen_t server_addr_len = sizeof(server_addr);

    if (-1 == bind(sock_fd , (struct sockaddr *)&server_addr,sizeof(server_addr)))
    {
        ERRLOG("套接字创建错误");
        close(sock_fd);
        return 0;
    }
    

    char buf[1024];

    memset(buf , 0 , sizeof(buf));

    struct sockaddr_in client_addr;
    socklen_t client_addr_len;

    while (1)
    {
        recvfrom(sock_fd , buf ,sizeof(buf), 0 ,(struct sockaddr *)&client_addr ,&client_addr_len);
        
        sendto(sock_fd , buf ,strlen(buf), 0 ,(struct sockaddr *)&client_addr ,client_addr_len);    
        
        printf("接收:%s\n",buf);

        memset(buf , 0 , sizeof(buf));
    }
    
    close(sock_fd);
    return 0;
}
